package com.hp.attendance.dao;

import com.hp.attendance.entity.Position;
import com.hp.attendance.entity.Position;
import com.hp.attendance.util.BaseDAO;

import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class PositionDao extends BaseDAO {
    //查询职称表
    public List<Position> getList() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "select * from position";
        List<Position> list = queryList(sql, new Object[0], new Position());
        return list;
    }
        
    //部门分页查询
    public List<Position> getPositionList(int pageNo, int pageSize) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from position limit ?,?";
        Object[] obj ={(pageNo-1)*pageSize,pageSize};
        List<Position> Positions = queryList(sql, obj, new Position());
        return Positions;
    }


    public long getTotal() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql ="select count(1) from position";
        int count = queryCount(sql, new Object[0]);
        return count;
    }

    public Position getPositionMAX() throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql = "SELECT * FROM `position` WHERE position_number=(SELECT MAX(position_number)FROM `position`)";
        Position position = queryOne(sql, new Object[0], new Position());
        return position;
    }
    public void PositionAdd(Position position) throws SQLException, IOException, ClassNotFoundException {
        String sql="insert into `position` values(null,?,?,?,?)";
        Object[] obj ={position.getPosition_number(),position.getName(),
                position.getLevel(),position.getNotes()};
        addOrUpdOrDel(sql,obj);
    }
    public void PositionDel(int id) throws SQLException, IOException, ClassNotFoundException {
        String sql="delete from `position` where id =?";
        Object[] obj = {id};
        addOrUpdOrDel(sql,obj);
    }
    public void PositionUpdate(Position position) throws SQLException, IOException, ClassNotFoundException {
        String sql="update `position` set position_number=?,name=?,level=?,notes=? where id =?";
        Object[] obj ={position.getPosition_number(),position.getName(),
                position.getLevel(),position.getNotes(),position.getId()};
        addOrUpdOrDel(sql,obj);
    }

    public Position positionPreUpd(int id) throws SQLException, IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        String sql="select * from `position` where id =?";
        Object[] obj={id};
        Position position = queryOne(sql, obj, new Position());
        return position;
    }
}
